<template>
  <div>
    <el-form :model="searchForm" label-width="120px" class="demo-dynamic" ref="tableSearchRef">
      <el-row>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" v-for="(val, key) in search" :key="search">
          <el-form-item :label="val.label" :prop="val.prop" :rules="[{ required: val.require, message: `${val.label}不能为空`, trigger: val.type === 'input' ? 'blur' : 'change' }]">
            <el-input v-model="searchForm[val.prop]" :placeholder="val.placeholder" clearable v-if="val.type === 'input'" style="width: 100%" />
            <el-date-picker v-model="searchForm[val.prop]" type="date" :placeholder="val.placeholder" v-else-if="val.type === 'date'" style="width: 100%" />
            <el-select v-model="searchForm[val.prop]" :placeholder="val.placeholder" v-else-if="val.type === 'select'" style="width: 100%">
              <el-option v-for="item in val.options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
          <el-form-item class="table-form-btn" label-width="30px">
            <!--            <template #label v-if="search.length > 1">-->
            <!--              <div class="table-form-btn-toggle ml10" @click="state.isToggle = !state.isToggle">-->
            <!--                <span>{{ state.isToggle ? '收筛选' : '展筛选' }}</span>-->
            <!--                <SvgIcon :name="state.isToggle ? 'ele-ArrowUp' : 'ele-ArrowDown'" />-->
            <!--              </div>-->
            <!--            </template>-->
            <div>
              <el-button size="default" type="primary" @click="onSearch(tableSearchRef)">查询 </el-button>
              <el-button size="default" type="info" class="ml10" @click="onReset(tableSearchRef)"> 重置 </el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, watch, computed } from 'vue'
import type { FormInstance } from 'element-plus'
import useModel from '@/composables/UseModel'

defineOptions({
  name: 'FSearch',
  inheritAttrs: false,
})
// const searchForm = ref<Record<string, any>>({})
// watch(
//   () => props.initForm,
//   (newVal) => {
//     searchForm.value = Object.assign({}, newVal!)
//   },
//   {
//     immediate: true,
//     deep: true,
//   }
// )
const props = defineProps<{
  search: searchType[]
  initForm?: Record<string, any>
}>()
const emit = defineEmits<{
  (e: 'search', val?: any): void
  (e: 'update:initForm', val: any): void
}>()

const searchForm: Record<string, any> = useModel(props, 'initForm', emit)

const tableSearchRef = ref<FormInstance>()
type searchType = {
  label?: string
  prop: string
  placeholder?: string
  require?: boolean
  type?: string
  [key: string]: any
}

const onSearch = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid: boolean) => {
    if (valid) {
      emit('search', searchForm.value)
    } else {
      return false
    }
  })
}
// 重置
const onReset = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  // console.log(searchForm.value)
  // searchForm.value = {}
  emit('search', searchForm.value)
}
</script>

<style scoped lang="scss"></style>
